package com.cqc.paoclient.ui.login;

import android.text.TextUtils;

import com.cqc.paoclient.http.RetrofitHelper;
import com.cqc.paoclient.model.BaseMsgModel;
import com.cqc.paoclient.util.NetErrorUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by cui on 2017/11/25.
 */

public class LoginPresenter extends LoginContract.Presenter {

    @Override
    void login(String user, String pwd) {
        if (TextUtils.isEmpty(user)) {
            mView.loginFail("用户名不能为空");
        } else if (TextUtils.isEmpty(pwd)) {
            mView.loginFail("密码不能为空");
        } else {
            disposable.add(RetrofitHelper.createUserApi().login(user, pwd)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<BaseMsgModel>() {
                        @Override
                        public void accept(BaseMsgModel baseMsgModule) throws Exception {
                            mView.loginSuccess(baseMsgModule);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            mView.loginFail(NetErrorUtil.handleThrowable(throwable));
                        }
                    }));
        }
    }
}
